﻿using personalplaner.business.enums;
using System;

namespace personalplaner.business.stamm
{
	public class DienstZeitEinheit : ItemBase
	{
		string _von;
        public string Von
        {
            get
            {
                return _von;
            }
            set
            {
                _von = value;
                RaisePropertyChanged("Von");
                RaisePropertyChanged("Zeit");
            }
        }

		string _bis;
		public string Bis
		{
			get
			{
				return _bis;
			}
			set
			{
				_bis = value;
				RaisePropertyChanged("Bis");
                RaisePropertyChanged("Zeit");
			}
		}

		enZeiteinheit _zeitTyp;
		public enZeiteinheit ZeitTyp
		{
			get
			{
				return _zeitTyp;
			}
			set
			{
				_zeitTyp = value;
				RaisePropertyChanged("ZeitTyp");
			}
		}

        public double Zeit
        {
            get
            {
                if (string.IsNullOrEmpty(Von) || string.IsNullOrEmpty(Bis))
                    return 0.0;

                int hVon;
                int mVon;
                int hBis;
                int mBis;

                var arrVon = Von.Split(':');
                if (arrVon.Length < 2)
                    return 0.0;

                int.TryParse(arrVon[0], out hVon);
                int.TryParse(arrVon[1], out mVon);

                var arrBis = Bis.Split(':');
                if (arrBis.Length < 2)
                    return 0.0;

                int.TryParse(arrBis[0], out hBis);
                int.TryParse(arrBis[1], out mBis);

                double von = ((double)hVon) + ((double)mVon / 60);
                double bis = ((double)hBis) + ((double)mBis / 60);
                if (von < bis)
                    return Math.Round(bis - von, 2);
                return Math.Round(von - bis, 2);
            }
        }
	}
}
